package com.jy.rhin.support.interceptor.convertor;

import com.jy.rhin.application.enums.*;
import com.jy.rhin.sdk.dto.audit.AuditSourceDto;
import com.jy.rhin.sdk.dto.audit.CodedValueTypeDto;
import org.oasis_open.docs.wsn.b_2.Notify;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @Description: 外部系统互相调用的审计日志
 */
@Service("AuditInfoConverter4TwoWayReferralApprovNotify")
public class AuditInfoConverter4TwoWayReferralApprovNotify extends BaseAuditInfoConverter<Notify, Notify> {

    public AuditInfoConverter4TwoWayReferralApprovNotify() {
        super(EventActionCodeEnum.READ,
                ServiceCodeEnum.TWO_WAY_REFERRAL,
                EventTypeCodeEnum.TWOWAY_REFERRAL_APPROVE_NOTIFY,
                true,
                ParticipantObjectIDTypeCodeEnum.SEARCH_CRITERIA,
                ParticipantObjectTypeCodeRoleEnum.RESOURCE,
                ParticipantObjectTypeCodeEnum.SYSTEM_OBJECT,
                ParticipantObjectDataLifeCycleEnum.ACCESS_USE);
    }

    /**
     * 审计源（审计源为事件目标）
     * @return
     */
    @Override
    List<AuditSourceDto> buildAuditSource() {
        AuditSourceDto auditSource = new AuditSourceDto();
        // TODO: 2022/5/24 必传 审计源的标识
        auditSource.setBiaoShi("terminology-service");
        // TODO: 从SOAP报文头获取<TO>节点，做为审计源网址
        auditSource.setWangZhi("http://localhost");
        CodedValueTypeDto auditSourceTypeCode = new CodedValueTypeDto();
        auditSourceTypeCode.setCodeSystem(CodeSystemEnum.CV_AUDIT_SOURCE_TYPE_CODE.getCode());
        auditSourceTypeCode.setCodeSystemName(CodeSystemEnum.CV_AUDIT_SOURCE_TYPE_CODE.getName());
        auditSourceTypeCode.setCode(AuditSourceTypeCodeEnum.WEB_SERVER_PROCESS.getCode());
        auditSourceTypeCode.setDisplayName(ServiceCodeEnum.TWO_WAY_REFERRAL.getName());
        auditSource.setLeiXing(auditSourceTypeCode);
        return Collections.singletonList(auditSource);
    }

    @Override
    AuditParticipantUserDesc buildSourceUser(Notify valueSetQueryRequest, Notify valueSetQueryResponse) {
        return new AuditParticipantUserDesc("client-1", "客户端");
    }

    @Override
    AuditParticipantUserDesc buildOriginatorUser(Notify valueSetQueryRequest, Notify valueSetQueryResponse) {
        return new AuditParticipantUserDesc("system", "admin");
    }

    @Override
    AuditParticipantUserDesc buildTargetUser(Notify valueSetQueryRequest, Notify valueSetQueryResponse) {
        return new AuditParticipantUserDesc("terminology-service", ServiceCodeEnum.TWO_WAY_REFERRAL.getName());
    }

    @Override
    String getObjectId(Notify valueSetQueryRequest, Notify valueSetQueryResponse) {
        return "";
    }

    @Override
    public boolean isMatch(String method, Object data) {
        if(data instanceof Notify){
            Notify notify = (Notify)data;
            return "NotificationConsumerPortImpl.notify".equals(method) && "rhin:TwoWayReferralAbort".equals(notify.getNotificationMessage().get(0).getTopic().getDialect());
        }
        return false;
    }
}
